<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 密码管理
 */
class PasswordController extends HomeController {
	private $password;
	private $category;
	/**
	 * 构造函数
	 */
	public function _initialize() {
		parent::_initialize();
		$this->category = M('Category');
		$this->password = M('Password');
	}
	/**
	 * 首页
	 */
	public function index() {
		$map = array();
		$map['uid'] = $this->uid;
		
		$list = $this->category->where($map)->field('id,parent_id,category_name,password_num')->order('order_num ASC')->select();
		$tree = new \Org\Util\Tree($list);
		// 最外层必须是双引号，变量需要替换为$id的形式
		$leaf = "<span><i class='icon icon-folder-open'></i>\$category_name(\$password_num)</span>&nbsp;&nbsp;";
		$leaf .= "<a href='". U('Password/add', array('id'=>'_id_')) ."' class='btn btn-mini'>添加密码</a> | ";
		$leaf .= "<a href='". U('Password/refer', array('id'=>'_id_')) ."' class='btn btn-mini'>查看密码</a>";
		$leaf = str_replace('_id_', '$id', $leaf);
		
		$branch = "<span><i class='icon icon-folder-close'></i>\$category_name(\$password_num)</span>&nbsp;&nbsp;";
		$branch .= "<a href='". U('Password/add', array('id'=>'_id_')) ."' class='btn btn-mini'>添加密码</a> | ";
		$branch .= "<a href='". U('Password/refer', array('id'=>'_id_')) ."' class='btn btn-mini'>查看密码</a>";
		$branch = str_replace('_id_', '$id', $branch);
		
		$treeview = $tree->get_treeview(0, 'pwd_tree" data-ride="tree', $leaf, $branch, 0, 'tree-lines');
		
		$this->assign('treeview', $treeview);
		$this->display();
	}
	/**
	 * 添加
	 */
	public function add() {
		if (IS_POST) {
			$post = I('post.');
			$this->_checkForm($post);
			
			$data = array();
			$data['uid'] = $this->uid;
			$data['category_id'] = $post['category_id'];
			$data['status'] = 1;
			$data['title'] = $post['title'];
			$data['user_name'] = pwd_encode($post['user_name'], $this->auth_code);
			$data['user_password'] = pwd_encode($post['user_password'], $this->auth_code);
			$data['create_time'] = get_time();
			$data['update_time'] = '0000-00-00 00:00:00';
			$data['delete_time'] = '0000-00-00 00:00:00';
			$data['create_ip'] = get_client_ip(0);
			$data['update_ip'] = '';
			$data['delete_ip'] = '';
			$data['remark'] = $post['remark'];
			$data['view_num'] = 0;
			
			if (false !== $this->password->add($data)) {
				$map_category = array();
				$map_category['id'] = $post['category_id'];
				$map_category['uid'] = $this->uid;
				
				$data_category = array();
				$data_category['password_num'] = array('exp', 'password_num+1');
				$this->category->where($map_category)->save($data_category);
				
				$this->success('密码添加成功');
			}else {
				$this->error('密码添加失败');
			}
		}else {
			$category_id = I('get.id');
			$map = array();
			$map['uid'] = $this->uid;
			
			$list = $this->category->where($map)->field('id,parent_id,category_name,password_num')->order('order_num ASC')->select();
			$tree = new \Org\Util\Tree($list);
			$str = "<option value='\$id' \$selected>\$spacer\$category_name</option>";
			$parent_id_options = $tree->get_tree(0, $str, $category_id);
			
			$this->assign('parent_id_options', $parent_id_options);
			$this->display();
		}
	}
	/**
	 * 检查数据的完整性
	 */
	protected function _checkForm($post) {
		$map = array();
		$map['id'] = $post['category_id'];
		$map['uid'] = $this->uid;
		$category_id = $this->category->where($map)->getField('id');
		if (is_null($category_id)) {
			$this->error('请选择类别');
		}
		if (empty($post['title'])) {
			$this->error('请填写标题');
		}
		if (empty($post['user_name'])) {
			$this->error('请填写用户名');
		}
		if (empty($post['user_password'])) {
			$this->error('请填写密码');
		}
		if (empty($post['remark'])) {
			$this->error('请填写备注');
		}
	}
	/**
	 * 按分类的密码列表
	 */
	public function refer() {
		$category_id = I('get.id');
		$p = I('get.p', 1, 'intval');
		$map = array();
		$map['uid'] = $this->uid;
		$map['category_id'] = $category_id;
		$map['status'] = 1;
		
		$count = $this->password->where($map)->count();
		$listRows = 20;
		$list = $this->password->where($map)->field('id,title,create_time,create_ip')->order('create_time DESC')->page("$p, $listRows")->select();
		
		$page = new \Org\Util\Page($count, $listRows);
		$show = $page->show();
		
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('p', $p);
		$this->assign('listRows', $listRows);
		$this->display();
	}
	/**
	 * 查看密码
	 */
	public function view() {
		$id = I('get.id');
		$map = array();
		$map['id'] = pwd_authcode($id);
		$map['uid'] = $this->uid;
		
		$data = $this->password->where($map)->field(true)->find();
		if (is_null($data)) {
			$this->error('参数错误');
		}
		$data['user_name'] = pwd_decode($data['user_name'], $this->auth_code);
		$data['user_password'] = pwd_decode($data['user_password'], $this->auth_code);
		
		// 获取类别名称
		$map_category = array();
		$map_category['id'] = $data['category_id'];
		$data['category_name'] = $this->category->where($map_category)->getField('category_name');
		
		// 更新查阅次数
		$data_password = array();
		$data_password['view_num'] = array('exp', 'view_num+1');
		$this->password->where($map)->save($data_password);
		
		$this->assign('data', $data);
		$this->display();
	}
	/**
	 * 删除密码
	 */
	public function delete() {
		$id = I('get.id');
		$map = array();
		$map['id'] = pwd_authcode($id);
		$map['uid'] = $this->uid;
		$map['status'] = 1;
		
		$data = $this->password->where($map)->field(true)->find();
		if (is_null($data)) {
			$this->error('参数错误');
		}
		$data_password = array();
		$data_password['status'] = 0;
		$data_password['delete_time'] = get_time();
		$data_password['delete_ip'] = get_client_ip(0);
		if (false !== $this->password->where($map)->save($data_password)) {
			$this->_updatePasswordNum($data['category_id'], $this->category, $this->password);
			$this->success('删除密码成功');
		}else {
			$this->error('删除密码失败');
		}
	}
}